#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    char buf[1024];
    int len;
    if (argc < 2) 
	{
		printf("Usage: %s -w <string>\n", argv[0]);
		printf("       %s -r\n", argv[0]);
		return -1;
	}

    int fd = open("/dev/hello", O_RDWR);
    if (fd < 0) {
        return -1;
    }

    // 写数据
    if ((strcmp(argv[1], "-w") == 0) && argc == 3) {
        len = strlen(argv[2]) + 1;
		len = len < 1024 ? len : 1024;
		write(fd, argv[2], len);
    } else if (strcmp(argv[1], "-r") == 0) {
        len = read(fd, buf, 1024);		
		buf[1023] = '\0';
		printf("APP read : %s\n", buf);
    }

    close(fd);

    return 0;
}